from datetime import datetime

DATE_FORMAT = '%Y-%m-%d %H:%M:%S'


def ts_date(ts: float):
    """
    时间戳 转 时间
    :param ts:
    :return:
    """
    return datetime.fromtimestamp(ts)


def ts_str(ts: float):
    """
    时间戳 转 日期
    :param ts:
    :return:
    """
    return datetime.fromtimestamp(ts).strftime(DATE_FORMAT) if ts else None


def str_to_ts(str: str) -> float:
    """
    日期 转 时间戳
    :param str:
    :return:
    """
    return datetime.strptime(str, DATE_FORMAT).timestamp()


def date_str(date: datetime):
    """
    格式化 日期
    :param date:
    :return:
    """
    return date.strftime(DATE_FORMAT) if date else None
